package cn.jufuns.util;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
public class CommonUtil {
	 private CommonUtil()
	    {
	    }

	    public static String addDoubleQuote(String src)
	    {
	        return addSymbol(src, "\"");
	    }

	    public static String addSingleQuote(String src)
	    {
	        return addSymbol(src, "'");
	    }

	    public static String addSymbol(String src, String symbol)
	    {
	        if(src == null || symbol == null)
	        {
	            throw new RuntimeException("src is null or symbol is null");
	        } else
	        {
	            StringBuilder sb = new StringBuilder();
	            sb.append(symbol).append(src).append(symbol);
	            return sb.toString();
	        }
	    }

	    public static String createArrayStr(String startStr, String endStr, String splitStr, boolean autoType, Object arrays[])
	    {
	        startStr = StringUtils.isBlank(startStr) ? "[" : startStr;
	        endStr = StringUtils.isBlank(endStr) ? "]" : endStr;
	        splitStr = StringUtils.isBlank(splitStr) ? "," : splitStr;
	        StringBuilder sb = new StringBuilder();
	        if(ArrayUtil.isNotEmpty(arrays))
	        {
	            Object aobj[];
	            int j = (aobj = arrays).length;
	            for(int i = 0; i < j; i++)
	            {
	                Object s = aobj[i];
	                if(autoType)
	                {
	                    if(s == null)
	                        makeupArrayStr(sb, s, splitStr);
	                    else
	                    if(JavaBase.isJavaString(s.getClass()) || JavaBase.isJavaDatetime(s.getClass()))
	                        makeupArrayStr(sb, addSingleQuote(s.toString()), splitStr);
	                    else
	                        makeupArrayStr(sb, s, splitStr);
	                } else
	                {
	                    makeupArrayStr(sb, s, splitStr);
	                }
	            }

	        }
	        return (new StringBuilder(String.valueOf(startStr))).append(sb.toString()).append(endStr).toString();
	    }

	    public static void makeupArrayStr(StringBuilder sb, Object currentStr, String splitStr)
	    {
	        if(sb == null)
	            throw new RuntimeException("sb is null");
	        splitStr = splitStr != null ? splitStr : ",";
	        if(sb.length() > 0)
	            sb.append(splitStr);
	        sb.append(currentStr);
	    }

	    public static String createInClause(Collection collection)
	    {
	        return createArrayStr("(", ")", ",", true, collection.toArray());
	    }

	    public static String createInClause(Object arrays[])
	    {
	        return createArrayStr("(", ")", ",", true, arrays);
	    }

	    public static String formatNumber(double v, int scale)
	    {
	        StringBuilder formatStr = new StringBuilder("#.");
	        if(Double.isNaN(v))
	            throw new IllegalArgumentException("v isNaN");
	        if(scale < 0)
	            throw new IllegalArgumentException("The scale must be a positive integer or zero");
	        for(int i = 0; i < scale; i++)
	            formatStr.append("0");

	        DecimalFormat df = new DecimalFormat(formatStr.toString());
	        String result = df.format(v);
	        if(result.indexOf(".") == 0)
	            result = (new StringBuilder("0")).append(result).toString();
	        return result;
	    }

	    public static float randomFloat(float start, float end)
	    {
	        if(start > end)
	        {
	            throw new RuntimeException("start>end");
	        } else
	        {
	            float rand = (float)(Math.random() * (double)(end - start));
	            return start + rand;
	        }
	    }

	    public static int randomInteger(int start, int end)
	    {
	        if(start > end)
	        {
	            throw new RuntimeException("start>end");
	        } else
	        {
	            int rand = (int)(Math.random() * (double)(end - start));
	            return start + rand;
	        }
	    }

	    public static long randomLong(long start, long end)
	    {
	        if(start > end)
	        {
	            throw new RuntimeException("start>end");
	        } else
	        {
	            long rand = (long)(Math.random() * (double)(end - start));
	            return start + rand;
	        }
	    }

	    public static String randomString(int start, int end, int size)
	    {
	        if(start > end)
	            throw new RuntimeException("start>end");
	        if(size <= 0)
	            throw new RuntimeException("size must >0");
	        int random = randomInteger(start, end);
	        String randomStr = String.valueOf(random);
	        int len = randomStr.length();
	        if(len < size)
	            randomStr = StringUtils.leftPad(randomStr, size, '0');
	        return randomStr;
	    }

	    public static int getMatchCount(String str, String regex)
	    {
	        return getMatchCount(str, regex, true);
	    }

	    public static int getMatchCount(String str, String regex, boolean caseInsensitive)
	    {
	        if(StringUtils.isBlank(str))
	            throw new RuntimeException("\u6307\u5B9A\u7684\u5B57\u7B26\u4E32\u4E0D\u80FD\u4E3A\u7A7A");
	        if(StringUtils.isBlank(regex))
	            throw new RuntimeException("\u6307\u5B9A\u7684\u6B63\u5219\u8868\u8FBE\u5F0F\u4E0D\u80FD\u4E3A\u7A7A");
	        int count = 0;
	        int flag = caseInsensitive ? 0 : 2;
	        Pattern p = Pattern.compile(regex, flag);
	        Matcher m = p.matcher(str);
	        for(int start = 0; m.find(start); start = m.end())
	            count++;

	        return count;
	    }

	    public static List getAllMatch(String str, String regex)
	    {
	        return getAllMatch(str, regex, true);
	    }

	    public static List getAllMatch(String str, String regex, boolean caseInsensitive)
	    {
	        if(StringUtils.isBlank(str))
	            throw new RuntimeException("\u6307\u5B9A\u7684\u5B57\u7B26\u4E32\u4E0D\u80FD\u4E3A\u7A7A");
	        if(StringUtils.isBlank(regex))
	            throw new RuntimeException("\u6307\u5B9A\u7684\u6B63\u5219\u8868\u8FBE\u5F0F\u4E0D\u80FD\u4E3A\u7A7A");
	        List resultList = new ArrayList();
	        int flag = caseInsensitive ? 0 : 2;
	        Pattern p = Pattern.compile(regex, flag);
	        Matcher m = p.matcher(str);
	        for(int start = 0; m.find(start); start = m.end())
	            resultList.add(m.group());

	        return resultList;
	    }

	    public static String getFirstMatch(String str, String regex)
	    {
	        return getFirstMatch(str, regex, true);
	    }

	    public static String getFirstMatch(String str, String regex, boolean caseInsensitive)
	    {
	        if(StringUtils.isBlank(str))
	            throw new RuntimeException("\u6307\u5B9A\u7684\u5B57\u7B26\u4E32\u4E0D\u80FD\u4E3A\u7A7A");
	        if(StringUtils.isBlank(regex))
	            throw new RuntimeException("\u6307\u5B9A\u7684\u6B63\u5219\u8868\u8FBE\u5F0F\u4E0D\u80FD\u4E3A\u7A7A");
	        int flag = caseInsensitive ? 0 : 2;
	        Pattern p = Pattern.compile(regex, flag);
	        Matcher m = p.matcher(str);
	        int start = 0;
	        if(m.find(start))
	            return m.group();
	        else
	            return null;
	    }

	    public static int getMatchIndex(String str, String regex)
	    {
	        return getMatchIndex(str, regex, true);
	    }

	    public static int getMatchIndex(String str, String regex, boolean caseInsensitive)
	    {
	        if(StringUtils.isBlank(str))
	            throw new RuntimeException("\u6307\u5B9A\u7684\u5B57\u7B26\u4E32\u4E0D\u80FD\u4E3A\u7A7A");
	        if(StringUtils.isBlank(regex))
	            throw new RuntimeException("\u6307\u5B9A\u7684\u6B63\u5219\u8868\u8FBE\u5F0F\u4E0D\u80FD\u4E3A\u7A7A");
	        int flag = caseInsensitive ? 0 : 2;
	        Pattern p = Pattern.compile(regex, flag);
	        Matcher m = p.matcher(str);
	        if(m.find())
	            return m.end();
	        else
	            return 0;
	    }

	    public static boolean isMatch(String str, String regex)
	    {
	        return isMatch(str, regex, true);
	    }

	    public static boolean isMatch(String str, String regex, boolean caseInsensitive)
	    {
	        if(StringUtils.isBlank(str))
	            throw new RuntimeException("\u6307\u5B9A\u7684\u5B57\u7B26\u4E32\u4E0D\u80FD\u4E3A\u7A7A");
	        if(StringUtils.isBlank(regex))
	        {
	            throw new RuntimeException("\u6307\u5B9A\u7684\u6B63\u5219\u8868\u8FBE\u5F0F\u4E0D\u80FD\u4E3A\u7A7A");
	        } else
	        {
	            int flag = caseInsensitive ? 0 : 2;
	            Pattern p = Pattern.compile(regex, flag);
	            Matcher m = p.matcher(str);
	            return m.matches();
	        }
	    }

	    public static boolean isChinese(char c)
	    {
	        return String.valueOf(c).matches("[\u4E00-\u9FA5]+");
	    }

	    public static boolean isChinese(String s)
	    {
	        return s.matches("[\u4E00-\u9FA5]+");
	    }

	    public static boolean isContainChinese(String s)
	    {
	        if(StringUtils.isBlank(s))
	            return false;
	        for(int i = 0; i < s.length(); i++)
	            if(isChinese(s.charAt(i)))
	                return true;

	        return false;
	    }

	    /**
	     * @deprecated Method getDefalutVal is deprecated
	     */

	    public static String getDefalutVal(Object obj, String defaultVal)
	    {
	        if(obj == null)
	            return defaultVal;
	        else
	            return obj.toString();
	    }

	    public static String getDefaultVal(Object obj, String defaultVal)
	    {
	        if(obj == null)
	            return defaultVal;
	        else
	            return obj.toString();
	    }

	    public static void p(Object obj)
	    {
	        if(obj == null)
	            System.out.println(obj);
	        else
	        if(obj.getClass().isArray())
	        {
	            String s = createArrayStr(null, null, ",", false, (Object[])obj);
	            System.out.println(s);
	        } else
	        {
	            System.out.println(obj);
	        }
	    }

	    public static Long isLong(String s, boolean isNullWhenException)
	    {
	        try
	        {
	            return Long.valueOf(Long.parseLong(StringUtils.trim(s)));
	        }
	        catch(Exception e)
	        {
	            if(isNullWhenException)
	                return null;
	            else
	                throw new RuntimeException(e);
	        }
	    }

	    public static Integer isInteger(String s, boolean isNullWhenException)
	    {
	        try
	        {
	            return Integer.valueOf(Integer.parseInt(StringUtils.trim(s)));
	        }
	        catch(Exception e)
	        {
	            if(isNullWhenException)
	                return null;
	            else
	                throw new RuntimeException(e);
	        }
	    }

	    public static Float isFloat(String s, boolean isNullWhenException)
	    {
	        try
	        {
	            return Float.valueOf(Float.parseFloat(StringUtils.trim(s)));
	        }
	        catch(Exception e)
	        {
	            if(isNullWhenException)
	                return null;
	            else
	                throw new RuntimeException(e);
	        }
	    }

	    public static Double isDouble(String s, boolean isNullWhenException)
	    {
	        try
	        {
	            return Double.valueOf(Double.parseDouble(StringUtils.trim(s)));
	        }
	        catch(Exception e)
	        {
	            if(isNullWhenException)
	                return null;
	            else
	                throw new RuntimeException(e);
	        }
	    }

	    public static String getStringSize(long size)
	    {
	        if(size == 0L)
	            return "0";
	        long b = size % 1024L;
	        long k = (size % 1048576L) / 1024L;
	        long m = (size % 1073741824L) / 1048576L;
	        StringBuffer sb = new StringBuffer();
	        if(m > 0L)
	            sb.append(m).append("M");
	        if(k > 0L)
	            sb.append(k).append("K");
	        if(b > 0L)
	            sb.append(b).append("B");
	        return sb.toString();
	    }

	    public static String join(String base, Object objs[])
	    {
	        base = getDefaultVal(base, "");
	        StringBuilder sBuilder = new StringBuilder(base);
	        if(ArrayUtil.isNotEmpty(objs))
	        {
	            Object aobj[];
	            int j = (aobj = objs).length;
	            for(int i = 0; i < j; i++)
	            {
	                Object obj = aobj[i];
	                sBuilder.append(obj);
	            }

	        }
	        return sBuilder.toString();
	    }

	    public static boolean isEmail(String email)
	    {
	        if(StringUtils.isBlank(email))
	            return false;
	        else
	            return isMatch(email, "^[A-Za-z0-9]{1}(\\.|-|_|\\w)+@\\w+(\\.\\w{2,3})+$");
	    }

	    public static Object convert(String v, Class clazz)
	    {
	        Object t = null;
	        if(JavaBase.isJavaNumber(clazz))
	        {
	            if(clazz == Integer.class || clazz == Integer.TYPE)
	                t = isInteger(v, true);
	            else
	            if(clazz == Long.class || clazz == Long.TYPE)
	                t = isLong(v, true);
	            else
	            if(clazz == Float.class || clazz == Float.TYPE)
	                t = isFloat(v, true);
	            else
	            if(clazz == Double.class || clazz == Double.TYPE)
	                t = isDouble(v, true);
	        } else
	        if(JavaBase.isJavaDatetime(clazz))
	        {
	            Date date = DateUtil.getYYYYMMDDHHMMSS(v);
	            if(clazz == Timestamp.class)
	                t = new Timestamp(date.getTime());
	        } else
	        {
	            t = v;
	        }
	        return t;
	    }

	    public static void main(String args1[])
	        throws IOException
	    {
	    }

	    public static final String EMAIL_ADDRESS_RE = "^[A-Za-z0-9]{1}(\\.|-|_|\\w)+@\\w+(\\.\\w{2,3})+$";
}
